<?php
abstract class controller {
	public $url, $root, $link, $self, $auth, $msg, $db, $usr;

	public function __construct($url, $auth = false){
		$this->root = "http://pos.phbusiness.eu/";
		$this->link = $this->root."index.php?url=";
		$this->self = implode("/", $url);
		$this->db = new database("localhost", "phbusine_pos", "Marketing123", "phbusine_pos");
		$this->msg = !empty($_GET["msg"]) ? $_GET["msg"] : null;
		$this->usr = !empty($_SESSION["usr"]) ? $_SESSION["usr"] : null;
		$this->url = $url;
		$this->auth = $auth;
	}

	public function redirect($to, $msg = null){
		if($msg != null){ $to .= "&msg=".$msg; }
		header("Location: ".$this->link.$to);
	}

	public function start(){
		$this->logout();
		$this->auth();
		$this->check();
		$this->collect();
		$this->run();
		$this->startGui();
	}

	private function logout(){
		if(isset($_GET["logout"])){
			unset($_SESSION["usr"]);
			$this->usr = null;
		}
	}

	private function auth(){
		if($this->auth){
			if($this->usr == null){ $this->redirect("login"); }
		}
	}

	public abstract function check();

	public abstract function collect();

	public abstract function run();

	private function startGui(){
		$gui = new gui($this);
		$gui->show();
	}
}
?>